reset LC_NUMERIC after Qt steps on it.
authortsteven4 <tsteven4@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Tue, 13 Jan 2015 13:27:38 +0000 (13:27 +0000)
committertsteven4 <tsteven4@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Tue, 13 Jan 2015 13:27:38 +0000 (13:27 +0000)
this had resulted in unicsv output using commas as the decimal separator in some locales.

gpsbabel/main.cc

index f67be4c31893ebf9da8705fc8d5c6782648b6ae3..5d2b7ed601476f374ba4aa1d5f361ddb797e0b11 100644 (file)
@@ -28,6 +28,7 @@
 #include "session.h"
 #include "src/core/usasciicodec.h"
 #include <ctype.h>
+#include <locale.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <signal.h>
@@ -252,6 +253,30 @@ main(int argc, char* argv[])
   QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
 #endif
 
+  // The first invocation of QTextCodec::codecForLocale() may result in LC_ALL being set to the native environment
+  // as opposed to the initial default "C" locale.
+  // This was demonstrated with Qt5 on Mac OS X.
+#ifdef DEBUG_LOCALE
+  printf("%s\n",setlocale(LC_ALL, NULL));
+#endif
+  (void) QTextCodec::codecForLocale();
+#ifdef DEBUG_LOCALE
+  printf("%s\n",setlocale(LC_ALL, NULL));
+#endif
+  // As recommended in QCoreApplication reset the locale to the default.
+  // Note the documentation says to set LC_NUMERIC, but QCoreApplicationPrivate::initLocale()
+  // actually sets LC_ALL.
+  // Perhaps we should restore LC_ALL instead of only LC_NUMERIC.
+  if (strcmp(setlocale(LC_NUMERIC,0), "C") != 0) {
+#ifdef DEBUG_LOCALE
+    printf("Resetting LC_NUMERIC\n");
+#endif
+    setlocale(LC_NUMERIC,"C");
+#ifdef DEBUG_LOCALE
+    printf("%s\n",setlocale(LC_ALL, NULL));
+#endif
+  }
+
   global_opts.objective = wptdata;
   global_opts.masked_objective = NOTHINGMASK;  /* this makes the default mask behaviour slightly different */
   global_opts.charset = NULL;
@@ -324,10 +349,10 @@ main(int argc, char* argv[])
     }
 
     switch (c) {
-      //case 'c':
-      //  optarg = argv[argn][2] ? argv[argn]+2 : argv[++argn];
-      //  cet_convert_init(optarg, 1);
-      //  break;
+    //case 'c':
+    //  optarg = argv[argn][2] ? argv[argn]+2 : argv[++argn];
+    //  cet_convert_init(optarg, 1);
+    //  break;
     case 'i':
       optarg = argv[argn][2]
                ? argv[argn]+2 : argv[++argn];
@@ -526,9 +551,9 @@ main(int argc, char* argv[])
       }
 
       break;
-      /*
-       * Undocumented '-vs' option for GUI wrappers.
-       */
+    /*
+     * Undocumented '-vs' option for GUI wrappers.
+     */
     case 'v':
       switch (argv[argn][2]) {
       case 's':
@@ -540,10 +565,10 @@ main(int argc, char* argv[])
       }
       break;
 
-      /*
-       * DOS-derived systems will need to escape
-       * this as -^^.
-       */
+    /*
+     * DOS-derived systems will need to escape
+     * this as -^^.
+     */
     case '^':
       disp_formats(opt_version);
       exit(0);